package components.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import client.Client;

import message.AuthenticationRequestMessage;
import message.InsertDataRequestMessage;
import message.Message;
import message.RestartServerRequestMessage;
import message.ShutDownServerRequestMessage;
import message.UpdateDataRequestMessage;

import communication.Communicator;
import components.AuthenticationContentPanel;

public class GoButtonActionListener implements ActionListener
{
	private final AuthenticationContentPanel panel;
	private final Client client;
	
	public GoButtonActionListener(AuthenticationContentPanel panel, Client client) throws InstantiationException
	{
		this.panel = panel;
		this.client = client;
	}
	private AuthenticationRequestMessage getAuthenticationMessage()
	{
		AuthenticationRequestMessage message = new AuthenticationRequestMessage();
		
		message.setUsername(panel.getUsername());
		message.setPassword(panel.getPassword());
		
		return message;
	}
	private InsertDataRequestMessage getCreateAccountMessage()
	{
		InsertDataRequestMessage message = new InsertDataRequestMessage();
		
		message.setUsername(panel.getUsername());
		message.setPassword(panel.getPassword());
		
		return message;
	}
	private UpdateDataRequestMessage getChangePasswordMessage()
	{
		UpdateDataRequestMessage message = new UpdateDataRequestMessage();
		
		message.setUsername(panel.getUsername());
		message.setPassword(panel.getPassword());
		
		return message;
	}
	private RestartServerRequestMessage getRestartServerMessage()
	{
		RestartServerRequestMessage message = new RestartServerRequestMessage();
		
		return message;
	}
	private ShutDownServerRequestMessage getShutDownServerMessage()
	{
		ShutDownServerRequestMessage message = new ShutDownServerRequestMessage();
		
		return message;
	}
	@Override
	public void actionPerformed(ActionEvent ae)
	{
		panel.setStatusText("Working . . . ");
		
		Message message = null;
		String action = panel.getAction();
		
		if (action.equals("Login"))
		{
			message = getAuthenticationMessage();
		}
		else if (action.equals("Create Account"))
		{
			message = getCreateAccountMessage();
		}
		else if (action.equals("Change Password"))
		{
			message = getChangePasswordMessage();
		}
		else if (action.equals("Restart Server"))
		{
			message = getRestartServerMessage();
		}
		else if (action.equals("Shutdown Server"))
		{
			message = getShutDownServerMessage();
		}
		
		client.getCommunicator().sendMessage(message);
	}
}
